﻿/*
 QuickReturn1.0
 JQuery plugin

 Created by: zhao jin
 Author email: a474197200@hotmail.com

 Last Update: March 27, 2014
*/
(function ($) {
    $.fn.QuickReturn = {
        show: function (opt) {
            var qcss = "<style type=\"text/css\">.quickreturn{position:fixed;z-index:100;overflow:hidden;cursor:pointer;display:none;}.quickreturn:hover{shadow}.quickreturnarr{width:0px;height:0px;border-style:solid;line-height:0px;margin:0px auto;}</style>";
            var qarrDom = "<div class=\"quickreturnarr\"></div>"; //箭头
            var qDom = "<div class=\"quickreturn\"></div>"; //按钮

            var opts = $.extend($.fn.QuickReturn.defaults, opt);
            $.fn.QuickReturn.defaults = opts;
            var s = opts.shadowH + "px " + opts.shadowV + "px " + opts.shadowR + "px " + opts.shadowC;
            s = "-moz-box-shadow:" + s + ";-webkit-box-shadow:" + s + ";box-shadow:" + s + ";";
            qcss = qcss.replace(/shadow/ig, s);

            $(qcss).appendTo("body");
            $(qDom).appendTo("body");
            $(qarrDom).appendTo($(".quickreturn"));

            $(".quickreturn").css({
                width: opts.width + "px",
                height: opts.height + "px",
                border: opts.bordersize + "px " + opts.borderstyle + " " + opts.bordercolor,
                "border-radius": opts.borderradius,
                "-webkit-border-radius": opts.borderradius,
                "-moz-border-radius": opts.borderradius,
                bottom: opts.bottom,
                right: opts.right,
                background: opts.background
            }).click(function () {
                $("html,body").animate({ scrollTop: 0 }, 200);
            });
            $(".quickreturnarr").css({
                "border-width": "0px " + opts.arrwidth / 2 + "px " + opts.arrheight + "px " + opts.arrwidth / 2 + "px",
                "border-color": "transparent transparent " + opts.arrbackground + " transparent",
                "_border-color": "#000000 #000000 " + opts.arrbackground + " #000000",
                "_filter": "progid:DXImageTransform.Microsoft.Chroma(color='#000000')"
            });
            var hh = $(".quickreturn").outerHeight() / 2 - $(".quickreturnarr").outerHeight() / 2;
            $(".quickreturnarr").css({
                "margin-top": hh + "px"
            });
            dragscroll();
        }
    };

    function dragscroll() {
        if (parseInt($(this).scrollTop()) > parseInt($.fn.QuickReturn.defaults.scrolltop)) {
            $(".quickreturn").show(500);
        }
        $(window).scroll(function () {
            if (parseInt($(this).scrollTop()) > parseInt($.fn.QuickReturn.defaults.scrolltop)) {
                $(".quickreturn").show();
                $("#header").css({
                    "top": parseInt($(this).scrollTop()) + "px"
                });
            } else {
                $(".quickreturn").hide();
                $("#header").removeAttr("style");
            }
        });
    };

    //默认属性配置
    $.fn.QuickReturn.defaults = {
        width: 50,               //按钮宽度
        height: 50,              //按钮高度
        bordersize: 1,           //按钮边框大小
        borderstyle: "solid",    //按钮边框类型
        bordercolor: "#60636D",  //按钮边框颜色
        borderradius: 25,        //按钮圆角大小
        bottom: 150,             //按钮距离浏览器底部位置
        right: 100,              //按钮距离浏览器右边位置
        background: "#333640",    //按钮背景颜色
        shadowH: 0,              //按钮模糊水平长度（鼠标移上后）
        shadowV: 0,              //按钮模糊垂直长度（鼠标移上后）
        shadowR: 30,             //按钮模糊半径（鼠标移上后）
        shadowC: "",             //按钮模糊颜色（鼠标移上后）
        scrolltop: 310,          //滚动条被拖动指定像素后显示按钮

        arrwidth: 30,            //箭头高度
        arrheight: 20,           //箭头高度
        arrbackground: "#ffffff" //箭头背景颜色
    };
})(jQuery);